package com.moon.admin.monitor.controller;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.moon.admin.sys.cache.UserCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.moon.admin.framework.domain.MoonPage;
import com.moon.admin.framework.mybatisplus.query.Condition;
import com.moon.admin.framework.secure.annotation.PreAuth;
import com.moon.admin.framework.springmvc.domain.R;
import com.moon.admin.framework.syslog.domain.SysOperLog;
import com.moon.admin.monitor.domain.query.SysLogQuery;
import com.moon.admin.sys.entity.SysUserEntity;
import com.moon.admin.monitor.service.SysLogService;
import com.moon.admin.sys.service.SysUserService;

import io.swagger.annotations.Api;

/**
 * 系统操作日志
 * 
 * @author ninglong
 */
@RestController
@RequestMapping("monitor/syslog")
@Api(value="系统操作日志",tags="系统操作日志")
public class SysLogController{
	
	@Autowired
	private SysLogService sysLogService;
	@Autowired
	private SysUserService userService;
	
	@PostMapping(value = "list")
	@PreAuth("sys_log_view")
	public R<MoonPage<SysOperLog>> list(@RequestBody SysLogQuery query) {
		LambdaQueryWrapper<SysOperLog> lwq = Condition.getQueryWrapper(new SysOperLog(), query);
		lwq.orderByDesc(SysOperLog::getOperTime);
		IPage<SysOperLog> page = sysLogService.page(Condition.getPage(query),lwq);
		List<SysOperLog> data = page.getRecords();
		data.forEach(entity->{
			SysUserEntity dept = UserCache.getUserById(entity.getUserId());
			entity.setUserName(dept==null?"":dept.getUserName());
		});
		return R.success(MoonPage.of(page));
	}
}